﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FrmAdd : Form
    {
        public FrmAdd()
        {
            InitializeComponent();
            this.cmbSex.SelectedIndex = 0;
        }
        public Form1 FrmParent { get; set; }
        //如果为1表示修改员工信息，如果为0表示添加员工信息
        public int Choice { get; set; }
        private void btnSave_Click(object sender, EventArgs e)
        {
            SE pr = new SE();
            if(Choice==0)
            {
            pr.ID = txtWorkNO.Text;
            pr.Age = Convert.ToInt32(txtAge.Text);
            if (this.cmbSex.SelectedItem.ToString() == "男")
            {
                pr.Gender = Gen.男;
            }
            if (this.cmbSex.SelectedItem.ToString() == "女")
            {
                pr.Gender = Gen.女;
            }
            pr.Name = txtName.Text.Trim();

            if (FrmParent.programmerList.Count != 0)
            {
                foreach (SE item in FrmParent.programmerList)
                {
                    if (item.ID == pr.ID)
                    {
                        MessageBox.Show("此工号已经存在，请重新输入");
                        return;
                    }
                }
            }
            
            FrmParent.programmerList.Add(pr);

            MessageBox.Show("保存成功");
            this.Close();

            this.FrmParent.BindGrid(FrmParent.programmerList);
                }
            if (Choice == 1)
            {
                bool flag=false;
                pr.ID = txtWorkNO.Text;
                 if (FrmParent.programmerList.Count != 0)
                {
                    foreach (SE item in FrmParent.programmerList)
                    {
                        if (item.ID == pr.ID)
                        {
                            flag = true;
                          item.Age= Convert.ToInt32(txtAge.Text);
                             if (this.cmbSex.SelectedItem.ToString() == "男")
                                {
                                    item.Gender = Gen.男;
                                }
                           if (this.cmbSex.SelectedItem.ToString() == "女")
                                {
                                    item.Gender = Gen.女;
                                }
                               item.Name = txtName.Text.Trim();
                               MessageBox.Show("修改成功");
                        }
                    }

                    if (flag == false)
                    {
                        MessageBox.Show("未找到所要修改的工号");
                    }
                    this.Close();

                    this.FrmParent.BindGrid(FrmParent.programmerList);
                            return;
                        }
                    }
                }

              

        private void FrmAdd_Load(object sender, EventArgs e)
        {

        }
    }
}
